﻿using System;

class SequenceOfEqualStrings
{
    static void Main()
    {
        //Exercise 3.
        //We are given a matrix of strings of size N x M. Sequences in the matrix we define as sets of several neighbor 
        //elements located on the same line, column or diagonal. Write a program that finds the longest sequence of 
        //equal strings in the matrix.

        ////Declare the matrix
        //Console.Write("Enter the matrix size N: ");
        //int n = int.Parse(Console.ReadLine());
        //Console.Write("Enter the matrix size M: ");
        //int m = int.Parse(Console.ReadLine());
        //string[,] matrix = new string[n, m];

        ////Initialize the matrix from the console
        //for (int row = 0; row < n; row++)
        //{
        //    for (int col = 0; col < m; col++)
        //    {
        //        Console.Write("Matrix [{0},{1}]  ", row, col);
        //        matrix[row, col] = Console.ReadLine();
        //    }
        //}

        string[,] matrix = new string[,] { { "ha", "fifi", "ho", "hi" }, 
                                            { "fo", "ha", "hi", "xx" }, 
                                            { "xxx", "ho", "ha", "xx" } };
        //string[,] matrix = new string[,] { { "s", "qq", "s" }, 
        //                                   { "pp", "pp", "s" }, 
        //                                   { "pp", "qq", "s" } };
        //string[,] matrix = new string[,] { { "pp", "qq", "s" }, 
        //                                   { "pp", "s", "pp" }, 
        //                                   { "s", "qq", "s" } };


        int counter = 1;
        int maxCounter = 1;
        string startPointOfMaxCount = " ";

        for (int row = 0; row < matrix.GetLength(0); row++)         //check by rows
        {
            for (int col = 0; col < matrix.GetLength(1) - 1; col++)
            {
                if (matrix[row, col] == matrix[row, col + 1])
                {
                    counter++;
                }
                else
                {
                    counter = 1;
                }
                if (counter > maxCounter)
                {
                    maxCounter = counter;
                    startPointOfMaxCount = matrix[row, col];
                }
            }
            counter = 1;
        }

        for (int col = 0; col < matrix.GetLength(1); col++)     //check by colomns
        {
            for (int row = 0; row < matrix.GetLength(0) - 1; row++)
            {
                if (matrix[row, col] == matrix[row + 1, col])
                {
                    counter++;
                }
                else
                {
                    counter = 1;
                }
                if (counter > maxCounter)
                {
                    maxCounter = counter;
                    startPointOfMaxCount = matrix[row, col];
                }
            }
            counter = 1;
        }

        for (int row = 0; row < matrix.GetLength(0) - 1; row++)     //check by right diagonal
        {
            for (int col = 0; col < matrix.GetLength(1) - 1; col++)
            {
                for (int rows = row, cols = col; rows < matrix.GetLength(0) - 1 && cols < matrix.GetLength(1) - 1; rows++, cols++)
                {
                    if (matrix[rows, cols] == matrix[rows + 1, cols + 1])
                    {
                        counter++;
                    }
                    else
                    {
                        counter = 1;
                    }
                    if (counter > maxCounter)
                    {
                        maxCounter = counter;
                        startPointOfMaxCount = matrix[rows, cols];
                    }
                }
                counter = 1;
            }                
        }

        for (int row = 0; row < matrix.GetLength(0) - 1; row++)     //check by left diagonal
        {
            for (int col = 1; col < matrix.GetLength(1); col++)
            {
                for (int rows = row, cols = col; rows < matrix.GetLength(0) - 1 && cols > 0; rows++, cols--)
                {
                    if (matrix[rows, cols] == matrix[rows + 1, cols - 1])
                    {
                        counter++;
                    }
                    else
                    {
                        counter = 1;
                    }
                    if (counter > maxCounter)
                    {
                        maxCounter = counter;
                        startPointOfMaxCount = matrix[rows, cols];
                    }
                }
                counter = 1;
            }
        }

        Console.WriteLine("The longest sequence of equal strings \"{0}\" in the matrix is {1}.", startPointOfMaxCount, maxCounter);

    }
}

